cmake_minimum_required(VERSION 3.14)

project(DcmImageQt VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

#set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)

if(MSVC)
    # 移除默认的/MD（动态链接）编译选项
    string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
    string(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
    string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
    string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_MINSIZEREL ${CMAKE_CXX_FLAGS_MINSIZEREL})
    string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELWITHDEBINFO ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})

    # 同样配置C编译器选项
#    string(REPLACE "/MD" "/MT" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
#    string(REPLACE "/MDd" "/MTd" CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
#    string(REPLACE "/MD" "/MT" CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
#    string(REPLACE "/MD" "/MT" CMAKE_C_FLAGS_MINSIZEREL ${CMAKE_C_FLAGS_MINSIZEREL})
#    string(REPLACE "/MD" "/MT" CMAKE_C_FLAGS_RELWITHDEBINFO ${CMAKE_C_FLAGS_RELWITHDEBINFO})
endif()

if(CMAKE_BUILD_TYPE MATCHES "Debug")
    message("Debug build")
    set(DCMTK_DIR D:/Work/OpenSource/dcmtk/3.6.9/install-vs2017-win32-mt/Debug/cmake)
    link_directories(D:/Work/OpenSource/dcmtk/3.6.9/install-vs2017-win32-mt/Debug/lib)
    set(OPENCV_LIB opencv_world460d)
else()
    message("Release build")
    set(DCMTK_DIR D:/Work/OpenSource/dcmtk/3.6.9/install-vs2017-win32-mt/Release/cmake)
    link_directories(D:/Work/OpenSource/dcmtk/3.6.9/install-vs2017-win32-mt/Release/lib)
    set(OPENCV_LIB opencv_world460)
endif()

find_package(DCMTK REQUIRED)
if (NOT DCMTK_FOUND)
    message("Skipping CMake build: ${DCMTK_NOT_FOUND_MESSAGE}")
    return ()
endif()

#message("DCMTK Libs: ${DCMTK_LIBRARIES}")
link_directories(ThirdPart/opencv460/lib)

include_directories(${DCMTK_INCLUDE_DIR})
include_directories(D:/Work/OpenSource/dcmtk/3.6.9/install-vs2017-win32-mt/include/dcmtk-suport)
include_directories(ThirdPart/opencv460/include)
set(PROJECT_SOURCES
        main.cpp
        DcmImageQt.qrc
        MainWindow.cpp
        MainWindow.h
        MainWindow.ui
        Displayer.cpp
        Displayer.h
        Displayer.ui
        DcmParser.cpp
        DcmParser.h
        CBaseImage.cpp
        CBaseImage.h
        CDicomImage.cpp
        CDicomImage.h
        DrawParam.cpp
        DrawParam.h
        Utilities.cpp
        Utilities.h
        dcmtk369.h

)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(DcmImageQt
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET DcmImageQt APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(DcmImageQt SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(DcmImageQt
            ${PROJECT_SOURCES}
        )
    endif()
endif()

target_link_libraries(DcmImageQt
    PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
    PRIVATE ${DCMTK_LIBRARIES}
    PRIVATE ${OPENCV_LIB}
)

set_target_properties(DcmImageQt PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(DcmImageQt)
endif()
